/**
 * Alipay.com Inc.
 * Copyright (c) 2004-2015 All Rights Reserved.
 */
package com.zb.jnlxc.aop;

import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.serializer.SerializerFeature;
import com.zb.jnlxc.constants.JsonConst;
import com.zb.jnlxc.model.Admin;
import org.apache.log4j.MDC;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.handler.HandlerInterceptorAdapter;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.util.UUID;

/**
 *
 * @author zhengbo.zb
 * @version $Id: LogInterceptor.java, v 0.1 2015-02-26 13:42 zhengbo.zb Exp $
 */
public class LoggerInterceptor extends HandlerInterceptorAdapter {
    Logger logger = LoggerFactory.getLogger(LoggerInterceptor.class);

    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler)
            throws Exception {
        HttpSession session = request.getSession();
        Admin user = (Admin) session.getAttribute("user");
        MDC.put("account",user==null?"":user.getAccount());
        MDC.put("threadUuid", UUID.randomUUID());
        String operator=request.getServletPath();
        logger.info("【请求】methord={} request={}", operator,JSONObject.toJSONString(request.getParameterMap(), JsonConst.defaultConfig,
                SerializerFeature.DisableCircularReferenceDetect));
        if (request.getServletPath().indexOf("/AdminAction/login")!=-1) {
            return true;
        }
        if (user == null){
            logger.warn("【会话信息丢失】");
            return false;
        }else {
            return true;
        }
    }
}
